﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Drawing;

using System.Threading;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;

namespace weiqijiance
{
    public class CDataClass
    {
        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;

        float[] m_Datafr;
        float[] m_Datafs;
   
        public bool LoadData(string sName)
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\标定数据.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            string sSet;
            sSet = ReadValue("标定", sName);
            if (sSet.Length <= 0)
            {
              
                return false;
            }
            string[] sbdsj = sSet.Split(';');
            string[] sbdfr = sbdsj[0].Split(',');
            string[] sbdfs = sbdsj[1].Split(',');
            m_Datafr = new float[sbdfr.Length-1];
            m_Datafs = new float[sbdfr.Length-1];
            for (int i = 0; i < sbdfr.Length-1; i++)
            {
                m_Datafr[i] =(float) Convert.ToDouble(sbdfr[i]);
                m_Datafs[i] = (float)Convert.ToDouble(sbdfs[i]);
            }
         
            return true;
        }

        public float GetZero()
        {
            if (m_Datafr == null)
            {
                return 0;
            }
            for (int i = 0; i < m_Datafr.Length; i++)
            {
                if (System.Math.Abs(m_Datafs[i] - 0) < 0.005)
                    return m_Datafr[i];
            }
            return 0;
        }
        public float GetValue(float fSourceData)
        {
            if (m_Datafr == null)
            {
                return 0;
            }
            int nSize = m_Datafr.Length;
            if (nSize < 2) return 0;
            int i = 0;
            for (i = 0; i < nSize; i++)
            {
                if (fSourceData < m_Datafr[i])
                    break;
            }

            float fValue;
            i = System.Math.Max(1, i);
            i = System.Math.Min(nSize - 1, i);

            fValue = m_Datafs[i - 1]
                + (fSourceData - m_Datafr[i - 1])
                * (m_Datafs[i] - m_Datafs[i - 1])
                / (m_Datafr[i] - m_Datafr[i - 1]);
            fValue=(float)((int)(fValue*10000))/10000;
            return fValue;
        }

        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        
        









    }
}
